package net.hzlj.platform.enterprise.socket.netty;

import android.content.Intent;
import android.util.Log;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.common.Constant;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Description: Netty客户端消息处理
 * @Author: Jason Zhang
 * @Date: 2017/06/28
 */

public class NettyClientHandler extends SimpleChannelInboundHandler<String> {

    public static final String TAG = NettyClientHandler.class.getSimpleName();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        Log.i(TAG,"客户端连接服务器成功");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext context, String str) throws Exception {
        Log.i(TAG,"接收来自服务器的消息:" + str);
        BootApp.getInstance().sendBroadcast(Constant.Intent.ACTION_SOCKET_MESSAGE_RECEIVED,str);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
        ctx.close();
    }

}
